package com.hjx.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.junit.Test;

import java.util.List;
import java.util.Map;

public class ParserJSON {

    @Test
    public void test(){
        List<Map> json = JSON.parseArray("[\n" +
                "  {\n" +
                "    \"method\": \"PsnAccBocnetQryCrcdStatement\",\n" +
                "    \"conversationId\": \"fb673ee4-3f3e-423a-bedb-ad25b92cd773\",\n" +
                "    \"params\": {\n" +
                "      \"statementMonth\": \"2018/01\",\n" +
                "      \"accountSeq\": \"44833791\"\n" +
                "    }\n" +
                "  }\n" +
                "]", Map.class);
        System.out.println(json);
    }

    @Test
    public void testFormat(){
        String word = "{\"request\":[{\"method\":\"PsnAccBocnetQryCrcdStatement\",\"conversationId\":\"572ce625-4a2e-4977-90a1-d585f4f7b1c4\",\"params\":{\"statementMonth\":\"2017/12\",\"accountSeq\":\"44833791\"}}],\"header\":{}}";
        JSONObject object = JSON.parseObject(word);
        System.out.println(JSON.toJSONString(object));
        System.out.println(JSON.toJSONString(object,
                SerializerFeature.PrettyFormat));

    }

    @Test
    public void testJSONNull(){
        String json = "{\"request\":[{\"method\":\"PsnAccBocnetQryLoginInfo\",\"conversationId\":null,\"params\":null}],\"header\":{}}";
        System.out.println(JSON.toJSONString(JSON.parseObject(json,Feature.IgnoreNotMatch), SerializerFeature.WriteMapNullValue));

        String json1 = "{\"a\":0,\"b\":null}"; System.out.println(JSON.parseObject(json));
        System.out.println(json1);

    }

    @Test
    public void testJSON(){
        String json = null;
        System.out.println(JSON.toJSON(json));
    }

    @Test
    public void testResult() throws IOException {
        String json = FileUtils.readFileToString(new File("D:\\ws\\testGradle\\src\\test\\resources\\result.json"), "UTF-8");

//        JSON.parseObject();
        System.out.println(StringEscapeUtils.unescapeJava(json));
    }
}

